package com.bmft.task01.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.bmft.task01.controller.Helper;
import com.bmft.task01.dao.impl.AnimalDaoImpl;
import com.bmft.task01.service.AnimalService;
import com.bmft.task01.util.enum_.DeleteEnum;
import com.bmft.task01.util.enum_.InsertEnum;
import com.bmft.task01.util.enum_.UpdateEnum;

;import java.util.Map;
import java.util.Scanner;


/**
 * This class let user input message and call the method of AnimalDao.
 *
 * @author Jarvan
 * @version 1.0
 * @create 2020/9/26 14:15
 */
public class AnimalServiceImpl implements AnimalService {
    private AnimalDaoImpl animalDao = new AnimalDaoImpl();
    private Scanner scanner = new Scanner(System.in);

    /**
     * Input a int by scanner
     *
     * @return int
     */
    @Override
    public int inputInt() {
        System.out.print("input your option: ");
        return Integer.parseInt(scanner.nextLine());
    }

    /**
     * input String by scanner.
     *
     * @return String that you input .
     */
    @Override
    public String inputString() {
        return scanner.nextLine();
    }

    /**
     * Return Map that you input the String format by json.
     *
     * @return Map that you input the String format by json.
     */
    @Override
    public Map<String, Object> inputMap() {

        Object parse = JSONObject.parse(scanner.nextLine());
        return (Map<String, Object>) parse;
    }

    /**
     * Insert page table
     */
    @Override
    public void insert() {
        displayAnimals();
        System.out.println("=====insert=======");
        System.out.println(InsertEnum.BACK_MAIN_MENU.getCode() + ". " + InsertEnum.BACK_MAIN_MENU.getMeans());
        System.out.println(InsertEnum.INSERT_ANIMAL.getCode() + ". " + InsertEnum.INSERT_ANIMAL.getMeans());
        System.out.println(InsertEnum.INSERT_CHARACTERISTIC.getCode() + ". " + InsertEnum.INSERT_CHARACTERISTIC.getMeans());

        int inputInt = inputInt();

        if (inputInt == InsertEnum.BACK_MAIN_MENU.getCode()) {
            // 0.
            Helper.start();
        } else if (inputInt == InsertEnum.INSERT_ANIMAL.getCode()) {
            // 1. insert total animal (create)
            System.out.println("input the name of the animal=");
            String animalName = inputString();
            System.out.println("input the Map of the " + animalName);
            System.out.println("example={\"颜色\":\"白色\",\"体重\":2.81}");
            Map<String, Object> animal = inputMap();
            animalDao.insertAnimal(animalName, animal);
        } else if (inputInt == InsertEnum.INSERT_CHARACTERISTIC.getCode()) {
            // 2. insert characteristic .
            System.out.println("input the name of the animal=");
            String animalName = inputString();
            if (animalDao.existAnimal(animalName)) {
                System.out.println("input  characteristic of " + animalName);
                String characteristicName = inputString();
                System.out.println("input the characteristic of " + characteristicName);
                String characteristic = inputString();
                animalDao.insertCharacteristic(animalName,characteristicName,characteristic);
            } else {
                System.err.println("animal name not exist in the map!");
                System.out.println();
                insert();
            }
        } else {
            // Other Situation.
            System.err.println("your date is illegal,please try again.");
            insert();
        }


    }

    /**
     * Delete page table
     */
    @Override
    public void delete() {
        displayAnimals();

        System.out.println("=====delete=======");
        System.out.println(DeleteEnum.BACK_MAIN_MENU.getCode() + ". " + DeleteEnum.BACK_MAIN_MENU.getMeans());
        System.out.println(DeleteEnum.DELETE_ANIMAL.getCode() + ". " + DeleteEnum.DELETE_ANIMAL.getMeans());
        System.out.println(DeleteEnum.DELETE_CHARACTERISTIC.getCode() + ". " + DeleteEnum.DELETE_CHARACTERISTIC.getMeans());

        int inputInt = inputInt();

        if (inputInt == DeleteEnum.BACK_MAIN_MENU.getCode()) {
            // 0.
            Helper.start();
        } else if (inputInt == DeleteEnum.DELETE_ANIMAL.getCode()) {
            // 1. delete total animal (create)
            System.out.println("input the name of the animal=");
            String animalName = inputString();
            animalDao.deleteAnimal(animalName);
        } else if (inputInt == DeleteEnum.DELETE_CHARACTERISTIC.getCode()) {
            // 2. delete characteristic .
            System.out.println("input the name of the animal=");
            String animalName = inputString();
            if (animalDao.existAnimal(animalName)) {
                System.out.println("input  characteristic of " + animalName);
                String characteristicName = inputString();
                animalDao.deleteCharacteristic(animalName, characteristicName);
            } else {
                System.err.println("animal name not exist in the map!");
                System.out.println();
                insert();
            }
        } else {
            // Other Situation.
            System.err.println("your date is illegal,please try again.");
            insert();
        }

    }

    /**
     * Update page table
     */
    @Override
    public void update() {
        displayAnimals();

        System.out.println("=====update=======");
        System.out.println(UpdateEnum.BACK_MAIN_MENU.getCode() + ". " + UpdateEnum.BACK_MAIN_MENU.getMeans());
        System.out.println(UpdateEnum.UPDATE_ANIMAL.getCode() + ". " + UpdateEnum.UPDATE_ANIMAL.getMeans());
        System.out.println(UpdateEnum.UPDATE_CHARACTERISTIC.getCode() + ". " + UpdateEnum.UPDATE_CHARACTERISTIC.getMeans());

        int inputInt = inputInt();

        if (inputInt == UpdateEnum.BACK_MAIN_MENU.getCode()) {
            // 0.
            Helper.start();
        } else if (inputInt == UpdateEnum.UPDATE_ANIMAL.getCode()) {
            // 1. update total animal (create)
            System.out.println("update the name of the animal=");
            String oldAnimalName = inputString();
            System.out.println("new Animal name=");
            String newAnimalName = inputString();
            animalDao.updateAnimalName(oldAnimalName, newAnimalName);

        } else if (inputInt == UpdateEnum.UPDATE_CHARACTERISTIC.getCode()) {
            // 2. update characteristic .
            System.out.println("update the name of the animal=");
            String animalName = inputString();
            if (animalDao.existAnimal(animalName)) {
                System.out.println("update  characteristic name of " + animalName);
                String characteristicName = inputString();
                System.out.println("update the characteristic of " + characteristicName + " =");
                String characteristic = inputString();
                animalDao.updateCharacteristic(animalName, characteristicName, characteristic);
            } else {
                System.err.println("animal name not exist in the map!");
                System.out.println();
                insert();
            }
        } else {
            // Other Situation.
            System.err.println("your date is illegal,please try again.");
            insert();
        }

    }

    /**
     * Query page table
     *
     * @return
     */
    @Override
    public Map<String, Map<String, Object>> queryAnimals() {
        return animalDao.queryAllAnimalList();
    }

    /**
     * Query characteristics
     *
     * @param animalName animal name
     * @return map of a animal
     */
    @Override
    public Map<String, Object> queryCharacteristics(String animalName) {
        return animalDao.queryCharacteristics(animalName);
    }

    /**
     * Display animals
     *
     * @return
     */
    @Override
    public void displayAnimals() {
        Map<String, Map<String, Object>> animals = queryAnimals();
        for (Map.Entry<String, Map<String, Object>> animal : animals.entrySet()) {
            System.out.println(animal);
        }
    }

    /**
     * Display characteristic of the animal name.
     *
     * @param animalName animal  name
     */
    @Override
    public void displayCharacteristics(String animalName) {
        Map<String, Object> animal = queryCharacteristics(animalName);
        System.out.println(animal);
    }


}
